<?php

namespace front\modules\web\controllers;

use backfeed\services\CommentService;
use EasyWeChat\Foundation\Application;
use front\controllers\WebBaseController;
use goods\services\CollectionService;
use goods\services\ProductService;
use goods\services\ShopCategoryProductService;
use goods\services\ShopProductCouponsService;
use wechat\services\AccountService;
use wechat\services\BannerService;
use wechat\services\ThemeService;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Controller;

class DefaultController extends WebBaseController
{

    public $layout = 'main';

    /**
     * 微信授权
     */
    public function actionAuthor()
    {
        $config = AccountService::getWechatConfig('gh_00df42447282');
        $app = new Application($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $openId = $user->getId();
        $_SESSION['openId'] = $openId;
        $_SESSION['token_time'] = time();
        $_SESSION['token'] = $user->getAccessToken();
        $_SESSION['appId'] = $config['app_id'];
        $_SESSION['appSecret'] = $config['secret'];
        $targetUrl = empty($_SESSION['target_url']) ? '/' : $_SESSION['target_url'];
        return $this->redirect($targetUrl);
    }

    /**
     *  首页
     * @return string
     */
    public function actionHome()
    {
        $session = \yii::$app->session;
        $shop_id = 1001;
        if(!isset($shop_id)){
            $shop_id = $session->get('shop_id');
        }
        $session->set('shop_id',$shop_id);
        //拉取Banner
        $banners = BannerService::getBanners();
        if($banners['code'] ==0){
            $data['banner'] = $banners['result'];
        }
        //拉取专题
        $theme = ThemeService::getTheme();
        if($theme['code'] ==0){
            $data['theme'] = $theme['result'];
        }
        //热门类目
        $category = ProductService::hotCategory($shop_id);
        foreach($category as $item){
            $data['cate'][$item['category_id']] = $item['category_name'];
        }
        //热门商品
        $data['hotPro'] = [];
        if(!empty($category)){
           $data['hotPro'] = static::getHotProduct($shop_id);
        }
        return $this->render('home',['data'=>$data]);
    }

    public function actionDiscart()
    {
        isset($_GET['type']) AND $type = intval($_GET['type']);
        $arr = array();
        $cache = \yii::$app->cache;
        $shop_id = $this->shop_id;
        $cache->set('shop_id',$shop_id,3600 * 24);
        if($type === 1){
            $arr['cate'] = [
                '0' => '全部',
                1 => '折扣',
                2 => '优惠券',
                3 => '兑用券',
            ];
            $arr['prod'] = static::getDiscartProduct($shop_id);

        }else{
            $category = ProductService::hotCategory($shop_id);
            $arr['cate'][0] = '全部';
            foreach($category as $item){
                $arr['cate'][$item['category_id']] = $item['category_name'];
            }
            $arr['prod'] = static::getHotProduct($shop_id);

            $arr['list0'] = array(

                '101'=> array('name'=>'羽绒服1', 'img'=>'/static/img/tmp008.jpg'),
                '102'=> array('name'=>'羽绒服2', 'img'=>'/static/img/tmp008.jpg')
            );

        }
        $arr['list1'] = array(
            '101' => array('name'=>'羽绒服折1','img'=>'/static/img/tmp010.jpg'),
            '102' => array('name'=>'羽绒服折2','img'=>'/static/img/tmp010.jpg'),
            '103' => array('name'=>'羽绒服折3','img'=>'/static/img/tmp010.jpg'),
            '104' => array('name'=>'羽绒服折4','img'=>'/static/img/tmp010.jpg')
        );

        $arr['list2'] = array(
            '101'=> array('name'=>'羽绒服折1', 'img'=>'/static/img/tmp009.jpg'),
            '102'=> array('name'=>'羽绒服折2', 'img'=>'/static/img/tmp009.jpg'),
            '103'=> array('name'=>'羽绒服折3', 'img'=>'/static/img/tmp009.jpg'),
            '104'=> array('name'=>'羽绒服折1', 'img'=>'/static/img/tmp009.jpg'),
            '105'=> array('name'=>'羽绒服折2', 'img'=>'/static/img/tmp009.jpg'),
            '106'=> array('name'=>'羽绒服折3', 'img'=>'/static/img/tmp009.jpg'),
            '107'=> array('name'=>'羽绒服折1', 'img'=>'/static/img/tmp009.jpg'),
            '108'=> array('name'=>'羽绒服折2', 'img'=>'/static/img/tmp009.jpg'),
            '109'=> array('name'=>'羽绒服折4', 'img'=>'/static/img/tmp009.jpg')
        );

        echo trim($_GET['funcName']).'('.Json::encode($arr).')';
    }

    /**
     * 获取热门商品
     */
    public function actionHotpro()
    {
        isset($_GET['type']) AND $type = intval($_GET['type']);
        $cache = \yii::$app->cache;
        $shop_id = $this->shop_id;
        if($type == 0){
            $arr = static::getHotProduct($shop_id);
        }else{
            $arr = static::getHotProduct($shop_id, $type);
        }

        echo trim($_GET['funcName']).'('.json_encode($arr).')';
    }

    public function actionDispro()
    {
        isset($_GET['type']) AND $type = intval($_GET['type']);
        $cache = \yii::$app->cache;
        $shop_id = $this->shop_id;
        if($type == 0){
            $arr = static::getDiscartProduct($shop_id);
        }else{
            $arr = static::getDiscartProduct($shop_id, $type);
        }

        echo trim($_GET['funcName']).'('.json_encode($arr).')';
    }
    /**
     *  根据店铺ID和类目ID获取商品列表
     * @param $shop_id
     * @param $catgegory_id
     * @return array
     */
    private static function getHotProduct($shop_id, $catgegory_id = null)
    {
        $hotPro = ShopCategoryProductService::hotProductList($shop_id, $catgegory_id);

        $hotPro = ArrayHelper::toArray($hotPro);
        yii::info($hotPro);
        $data = [];
        foreach($hotPro as $key => $pro){
//            $product['thumb'] = static::getImgPath($pro['thumb']);
            $pro = ProductService::ProductInfo($pro['pid']);
            if($pro['code'] != 0){
                yii::info($pro);
                continue;
            }
            if($pro['code'] == 0){
                $product = ArrayHelper::toArray($pro['result']);
                $pro = $product['productInfo'];
                if($pro['stock'] == 0 || $pro['status'] == 0){
                    continue;
                }
                $product = [];
                $product['pid'] = $pro['pid'];
                $product['name'] = $pro['name'];
                $product['desc'] = $pro['name'];
                $product['price'] = static::price_format($pro['price']);
                $product['img'] = $pro['thumb'][0] . '@1e_1c_0o_0l_150h_150w_100q.jpg';
//                $product['img'] = 'http://backend.shopbox.com' . $product['thumb'];
            }
            $product['coll'] = CollectionService::getCollectionCount($pro['pid']);
            $params = [
                'product_id' => $pro['pid'],
                'getByWeb' => 1
            ];
            $comm = CommentService::getCount($params);
            $product['comm'] = 0;
            if($comm['code'] == 0){
                $product['comm'] = $comm['result']['count'];
            }
            array_push($data, $product);
        }
        return $data;
    }

    /**
     *  根据店铺ID和优惠类型获取商品列表
     * @param $shop_id
     * @param $type
     * @return array
     */
    private static function getDiscartProduct($shop_id, $type = null)
    {
        $discartPro = ShopProductCouponsService::couponsProductList($shop_id, $type);
        $discartPro = ArrayHelper::toArray($discartPro);
        $data = [];
        if(is_array($discartPro))
        {
            foreach($discartPro as $pro){
                $product = ProductService::ProductInfo($pro['pid']);
                if($product['code'] == 0){
                    $product = ArrayHelper::toArray($product['result']);
                    $product = $product['productInfo'];
                    if($product['stock'] == 0 || $product['status'] == 0){
                        continue;
                    }
                    $dis['pid'] = $product['pid'];
                    $dis['name'] = $product['name'];
                    $dis['desc'] = $product['name'];
                    $dis['price1'] = static::price_format($product['price']);
                    $dis['price2'] = static::price_format($product['price']);
                    $dis['note'] = 100.00;
                    $dis['cls'] = 'c1';
                    $dis['img'] = $product['thumb'][0] . '@1e_1c_0o_0l_150h_150w_100q.jpg';
                    array_push($data, $dis);
                }
            }
        }
//        print_r($data);
        return $data;
    }

}
